package com.lqian.blog.service;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lqian.blog.dao.mapper.ArticleMapper;
import com.lqian.blog.dao.pojo.Article;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

@Component
public class ThreadService {
    //期望此操作在线程池 执行 不会影响原有的主线程
    @Async("taskExecutor") //开启指定字符名字的taskExecutor执行器
    public void updateViewCount(ArticleMapper articleMapper, Article article) {

        Article articleUpdate = new Article();
        articleUpdate.setViewCounts(article.getViewCounts() + 1);
        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Article::getId, article.getId());
        //设置一个 为了在多线程的环境下 线程安全
        queryWrapper.eq(Article::getViewCounts, article.getViewCounts());
        // update article set view_count=100 where view_count=99 and id=11
        //UPDATE ms_article SET comment_counts=?, view_counts=?, weight=? WHERE (id = ? AND view_counts = ?)
        articleMapper.update(articleUpdate, queryWrapper);
//        try {
//            //睡眠5秒 证明不会影响主线程的使用
//            Thread.sleep(5000);
//            System.out.println("更新完成了....");
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
    }
}
